package com.thrd.basicframework.bean;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

/**
 * @author: ${唐杰}
 * @date: 2021/3/17
 */
public abstract class BaseRecyclerAdapter <T> extends RecyclerView.Adapter<BaseRecyclerAdapter.BaseViewHolder> {

    //上下文环境
    private Context context;
    //数据集合
    private List<T> data;
    private final LayoutInflater mLayoutInflater;
    private ItemClick itemClick;
    /**
     * 刷新数据
     *
     * @param data
     */
    public void refresh(List<T> data) {
        this.data = data;
        notifyDataSetChanged();
    }



    /**
     * 添加数据
     *
     * @param data
     */
    public void append(List<T> data) {
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    public BaseRecyclerAdapter(Context context, List<T> data) {
        this.context = context;
        this.data = data;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       int layoutId= getItemLayoutId();
        if(layoutId!=0){
            View view = mLayoutInflater.inflate(getItemLayoutId(),parent,false);
            return new BaseViewHolder(view);
        }

        if( getItemView()!=null){
            return new BaseViewHolder(getItemView());
        }

        //  View.inflate()
        // View view = LayoutInflater.from(context).inflate(getItemLayoutId(), parent, false);
        return null;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerAdapter.BaseViewHolder holder, final int position) {
        if(holder==null) return;
        final T t = data.get(position);
        bindData(holder, t);
        if(itemClick!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    itemClick.onItemClick(position,t);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    /**
     * 将数据绑定到itemView视图上
     *
     * @param holder
     * @param t
     */
    public abstract void bindData(BaseViewHolder holder, T t);

    /**
     * 子类通过重写此方法设置itemView项的布局视图
     *
     * @return
     */
    public abstract int getItemLayoutId();

    /**
     * 子类通过重写此方法设置itemView项的布局视图
     *
     * @return
     */
    public View getItemView(){
        return null;
    };

    /**
     * 封装 ViewHolder
     */
    public class BaseViewHolder extends RecyclerView.ViewHolder {

        // item 中的控件缓存在这里
        private SparseArray<View> views;
        public BaseViewHolder(View itemView) {
            super(itemView);
            views = new SparseArray<>();

        }

        /**
         * 获取 itemView 中的控件
         *
         * @param id
         * @return
         */
        public View getView(int id) {
            View view = views.get(id);
            if (view == null) {
                view = itemView.findViewById(id);
                views.put(id, view);
            }
            return view;
        }

    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }


    public ItemClick getItemClick() {
        return itemClick;
    }

    public void setItemClick(ItemClick itemClick) {
        this.itemClick = itemClick;
    }

    public interface ItemClick<T>{
        void onItemClick(int p,T e);
    }

    public LayoutInflater getmLayoutInflater() {
        return mLayoutInflater;
    }


}
